package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    private final CacheService cacheService;

    private final KafkaTemplate<String,String> kafkaTemplate;
    /**
     * 点赞
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.校验数据
        if (dto == null || dto.getArticleId() == null || checkParam(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.构建消息对象
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        //3.点赞 保存数据
        if (dto.getOperation() == 0){
            //判断是否已经点赞
            Object obj = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + String.valueOf(dto.getArticleId()), String.valueOf(user.getId()));
            if (obj != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "已点赞");
            }
            //保存当前key
            log.info("保存当前key:{},{},{}",dto.getArticleId(),user.getId(),dto);
            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + String.valueOf(dto.getArticleId()), String.valueOf(user.getId()), JSON.toJSONString(dto));
            mess.setAdd(1);
        }else {
            //删除当前key
            log.info("删除当前key:{},{}",dto.getArticleId(),user.getId());
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + String.valueOf(dto.getArticleId()), String.valueOf(user.getId()));
            mess.setAdd(-1);
        }
        //4.发送消息,数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 校验数据
     * @param dto
     * @return
     */
    private boolean checkParam(LikesBehaviorDto dto) {
        if (dto.getOperation()>1 || dto.getOperation() < 0 || dto.getType()>2 || dto.getType() < 0){
            return true;
        }
        return false;
    }
}
